package com.darren.euc.service.impl;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.darren.euc.dao.PrintRecordDao;
import com.darren.euc.entity.PageBean;
import com.darren.euc.entity.PrintRecord;
import com.darren.euc.service.PrintRecordService;
import com.darren.euc.util.StringUtil;

import net.sf.json.JSONObject;


@Service
public class PrintRecordServiceImpl implements PrintRecordService {
	
	private static final Logger log = Logger.getLogger(PrintRecordServiceImpl.class);
	
	@Autowired
    private PrintRecordDao printRecordDao;
	

	@Override
	public List<PrintRecord> getAll() {
		return printRecordDao.getAll();
	}
	
    @Override
    public PrintRecord save(PrintRecord printRecord) {
    	PrintRecord dbRecord = printRecordDao.findByJobId(printRecord.getJobId());
    	if(dbRecord != null){
    		return dbRecord;
    	}
        return printRecordDao.save(printRecord);
    }

    @Override
    public PrintRecord update(PrintRecord printRecord) {
        return printRecordDao.update(printRecord);
    }
    
    @Override
    public PrintRecord getLastRecords(){
    	return printRecordDao.getLastRecords();
    }
    
    
    @Override
    public void delete(PrintRecord printRecord) {
    	printRecordDao.delete(printRecord);
    }
    
    @Override
    public void updateMemberInfo(){
    	printRecordDao.updateMemberInfo();
    }
	
	@SuppressWarnings("rawtypes")
	public PageBean queryPrintRecordForPageUseHQL(JSONObject requestJson,Integer pageSize, Integer currentPage){		
		String HQL = null;
		String likes = null;
		String equals = null;
		List<Object> parameters = new ArrayList<Object>();
		JSONObject likeJson = requestJson.getJSONObject("like");
		Iterator likeIterator = likeJson.keys();
		while(likeIterator.hasNext()){
			String key = (String) likeIterator.next();
			key = StringUtil.convertNullToEmptyString(key);
			String value = likeJson.getString(key);
			value = StringUtil.convertNullToEmptyString(value);
			if(!"".equals(value)){
				likes = (likes == null?"": likes + " OR ") +  key + " like ? ";
				parameters.add("%"+value+"%");
			}
		}
		HQL = likes == null?null:(" (" + likes + ") ");		
		JSONObject equalsJson = requestJson.getJSONObject("equals");
        Iterator equalsIterator = equalsJson.keys();        
		while(equalsIterator.hasNext()){
			String key = (String) equalsIterator.next();
			key = StringUtil.convertNullToEmptyString(key);
			String value = equalsJson.getString(key);
			value = StringUtil.convertNullToEmptyString(value);
			if(!"".equals(value)){
				if("startTime".equals(key)){
					DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
					try {
						Date startDate = (Date)format.parse(value + " 00:00:00");
						parameters.add(startDate);
					} catch (ParseException e) {
						log.error(e.getMessage(),e);
					} 
					equals = (equals == null?"": ""+equals + " and ") +  " jobInTime >= ? ";					
				}else if("endTime".equals(key)){
					DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
					try {
						Date endDate = (Date)format.parse(value + " 23:59:59");
						parameters.add(endDate);
					} catch (ParseException e) {
						log.error(e.getMessage(),e);
					} 
					equals = (equals == null?"": ""+equals + " and ") +  " jobInTime <= ? ";					
				}else if("spaceId".equals(key) || "clientId".equals(key) || "memberId".equals(key)){
					equals = (equals == null?"": ""+equals + " and ") +  key + " = ? ";
					parameters.add(Integer.parseInt(value));
				}else{
					equals = (equals == null?"": ""+equals + " and ") +  key + " = ? ";
					parameters.add(value);
				}				
			}
		}		
		if(HQL != null){
			if(equals != null){
				HQL = HQL + " and " + equals;
			}
		}else{
			HQL = equals;
		}
		HQL = " from PrintRecord where client is not null " + (HQL == null?"":" and  " + HQL) + " order by jobInTime desc ";
		return printRecordDao.queryForPageByHQL(HQL, pageSize, currentPage,parameters.toArray());
	}



}